Skip to content

git: remove repo from SCM when workspace folder is removed - #327124

Open
Shreyash (Iitian001) wants to merge 3 commits into
microsoft:mainfrom
Iitian001:fix/git-remove-folder-removes-repo
Open

Shreyash (Iitian001) wants to merge 3 commits into
microsoft:mainfrom
Iitian001:fix/git-remove-folder-removes-repo

Conversation

@Iitian001

@Iitian001 Shreyash (Iitian001) commented Jul 23, 2026 •

Copy link
Copy Markdown

Summary

Fixes #108757

When a workspace folder is removed from a multi-root workspace, the corresponding git repository was not removed from the Source Control view.

Root cause

onDidChangeWorkspaceFolders looked up repositories to dispose with getOpenRepository(folder.uri), which resolves a repository containing the folder URI. That misses repositories rooted inside the removed folder and does not account for repositories opened above it (for example with git.openRepositoryInParentFolders).

Fix

The handler now walks this.openRepositories and disposes a repository when its root equals, contains, or is contained by a removed workspace folder, unless:

  • it is still active for an open editor, or
  • it still overlaps a remaining workspace folder (so a parent repository shared with another folder stays open).

Manually closed repository markers (closedRepositories) are intentionally left untouched, so a repository the user closed is not reopened by the follow-up scan.

Testing

  1. Create a multi-root workspace with two folders, each containing a git repository.
  2. Confirm both repositories appear in Source Control.
  3. Remove one folder (right-click > Remove Folder from Workspace).
  4. The repository for that folder disappears from Source Control; the other remains.
  5. With a parent repository shared by two workspace folders, removing one folder keeps the repository open.

When a workspace folder is removed from a multi-root workspace, the
corresponding git repository was not being removed from the SCM panel.

The original code used getOpenRepository(folder.uri) to find repos to
dispose, which internally relies on isDescendant(repoRoot, resourcePath).
This check fails when the repo root IS the workspace folder (i.e. the
most common case), because a path is not a descendant of itself.

Fix by iterating all open repositories and checking whether their root
is equal to or a descendant of any removed workspace folder, rather than
relying on the indirect URI-based lookup.

Additionally, prune stale closedRepositories workspace state entries
for any removed workspace folders. Without this, repos that were manually
closed before the folder was removed would ghost back into the SCM panel
the next time the workspace is opened.

Fixes microsoft#108757
Copilot AI review requested due to automatic review settings July 23, 2026 13:11
@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Ladislau Szomoru (@lszomoru)

Matched files:

  • extensions/git/src/model.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates Git repository lifecycle handling when workspace folders are removed.

Changes:

  • Disposes repositories associated with removed folders.
  • Prunes closed-repository workspace state.

Comment thread extensions/git/src/model.ts Outdated
Comment on lines +484 to +487
const openRepositoriesToDispose = this.openRepositories
.filter(r => removed.some(folder => isDescendant(folder.uri.fsPath, r.repository.root) || r.repository.root === folder.uri.fsPath))
.filter(r => !activeRepositories.has(r.repository))
.filter(r => !(workspace.workspaceFolders || []).some(f => isDescendant(f.uri.fsPath, r.repository.root)));
Comment thread extensions/git/src/model.ts Outdated
for (const folder of removed) {
for (const closedRepo of this._closedRepositoriesManager.repositories) {
if (isDescendant(folder.uri.fsPath, closedRepo) || closedRepo === folder.uri.fsPath) {
this._closedRepositoriesManager.deleteRepository(closedRepo);
@Iitian001

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Git - Removing a folder from multi-root workspace does not remove repo

3 participants